package org.hentai.bt.result;

import org.hentai.bt.DbConnectionMsg;
import org.hentai.bt.util.sql.SqlConnection;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ResultList<T> {

    private List<T> resultBeans;

    private ResultList(List<T> resultBeans) {
        this.resultBeans = resultBeans;
    }

    public List<T> getResultBeans() {
        return resultBeans;
    }

    public static <B> ResultList<B> getTable(B tableBean) throws SQLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        Class<?> beanClass = tableBean.getClass();

        Field[] fields = beanClass.getDeclaredFields();

        String sql = queryField(beanClass.getSimpleName(), fields);
        System.out.println(sql);

        Connection conn = SqlConnection.getConn();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        List<B> results = new ArrayList<>();
        while (rs.next()) {
            B result = (B) beanClass.getDeclaredConstructor().newInstance();

            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getType() == Integer.class) {
                    field.set(result, rs.getInt(field.getName()));
                } else if (field.getType() == Long.class) {
                    field.set(result, rs.getLong(field.getName()));
                } else if (field.getType() == Short.class) {
                    field.set(result, rs.getShort(field.getName()));
                } else if (field.getType() == Byte.class) {
                    field.set(result, rs.getByte(field.getName()));
                } else if (field.getType() == Float.class) {
                    field.set(result, rs.getFloat(field.getName()));
                } else if (field.getType() == Double.class) {
                    field.set(result, rs.getDouble(field.getName()));
                } else if (field.getType() == String.class) {
                    field.set(result, rs.getString(field.getName()));
                } else if (field.getType() == Boolean.class) {
                    field.set(result, rs.getBoolean(field.getName()));
                } else {
                    field.set(result, rs.getObject(field.getName()));
                }
            }
            results.add(result);

        }

        return new ResultList<>(results);
    }

    private static String queryAllField(String tableName) {
        return String.format("SELECT * FROM %s", tableName);
    }

    private static String queryField(String tableName, Field[] fields) {

        StringBuilder sql2 = new StringBuilder("SELECT ");
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            sql2.append(name);
            if (i < fields.length - 1) {
                sql2.append(",");
            }
        }
        sql2.append(" FROM ");
        sql2.append(tableName);
        return sql2.toString();
    }

}
